package com.gomore.opple.module.cards.used;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.model.EventReflashUsedFragment;
import com.gomore.opple.module.BaseFragmentV4;
import com.gomore.opple.module.cards.adapter.CardsAdapter;
import com.gomore.opple.module.cards.adapter.UsedCardsAdapter;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.widgets.PullBaseView;
import com.gomore.opple.widgets.PullRecyclerView;

import butterknife.Bind;
import de.greenrobot.event.EventBus;


/**
 * Created by robot
 */
public class UsedFragment extends BaseFragmentV4 implements UsedContract.View,
        PullBaseView.OnRefreshListener {

    @Bind(R.id.recyclerView)
    PullRecyclerView recyclerView;
    @Bind(R.id.empty_view)
    View empty_view;
    private UsedContract.Presenter mPresenter;
    private UsedCardsAdapter usedCardsAdapter;

    public static UsedFragment getInstance() {
        return new UsedFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_used;
    }

    @Override
    protected void initalizeData() {
        EventBus.getDefault().register(this);
        mPresenter.queryJDE(false);
    }

    @Override
    protected void initalizeViews() {
        usedCardsAdapter = new UsedCardsAdapter(getActivity(), getActivity(), R.layout.used_cards_item, mPresenter.getData());
        recyclerView.setLayoutManager(new LinearLayoutManager(UsedFragment.this.getActivity()));
        recyclerView.setAdapter(usedCardsAdapter);
        recyclerView.setOnRefreshListener(this);
    }


    @Override
    public void setPresenter(UsedContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    /**
     * 加载完毕
     */
    @Override
    public void showLoadMoreCompleted() {
        recyclerView.onFooterRefreshComplete();
    }

    /**
     * 刷新完毕
     */
    @Override
    public void showRefreshCompleted() {
        recyclerView.onHeaderRefreshComplete();
    }

    @Override
    public void showContentView() {
        usedCardsAdapter.notifyDataSetChanged();
        if (mPresenter.getData().size() == 0) {
            empty_view.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            empty_view.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onHeaderRefresh(PullBaseView view) {
        mPresenter.queryJDE(false);
    }

    @Override
    public void onFooterRefresh(PullBaseView view) {
        mPresenter.queryJDE(true);
    }

    public void onEventMainThread(EventReflashUsedFragment eventReflashUsedFragment) {
        if (eventReflashUsedFragment != null && eventReflashUsedFragment.isReflash()) {
            mPresenter.queryJDE(false);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
